package com.otdemo.util;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class MyDateUtil {
	
	/**
	 * 字符串转java.util.Date
	 * @param UtilDateString
	 * @param pattern
	 * @return
	 */
	public static java.util.Date getUtilDate(String UtilDateString,String pattern){
		DateFormat df=new SimpleDateFormat(pattern);
		try {
			return df.parse(UtilDateString);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	

	/**
	 * 字符串转java.sql.Date(不能直接转，需要先转成java.util.Date)
	 * @param sqlDateString
	 * @param pattern
	 * @return
	 * @throws ParseException
	 */
	public static java.sql.Date getSqlDate(String sqlDateString,String pattern) throws ParseException{
		java.util.Date utilDate=getUtilDate(sqlDateString, pattern);
		return new java.sql.Date(utilDate.getTime());
	}
	
	
	/**
	 * 字符串转localDate
	 * @param localDateString
	 * @param pattern
	 * @return
	 */
	public static LocalDate  getLocalDate(String localDateString,String pattern) {
		DateTimeFormatter dtFormatter=DateTimeFormatter.ofPattern(pattern);
		return LocalDate.parse(localDateString,dtFormatter);
	}
	
	
	/**
	 * 字符串转localDateTime
	 * @param localDateTimeString
	 * @param pattern
	 * @return
	 */
	public static LocalDateTime getLocalDateTime(String localDateTimeString,String pattern) {
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern, Locale.US);
		return LocalDateTime.parse(localDateTimeString, formatter);
	}
	
	
	/**
	 * 字符串转java.sql.timestamp
	 * @param timeStampString
	 * @return
	 */
	public static java.sql.Timestamp getSqlTimestamp(String timeStampString){
		return Timestamp.valueOf(timeStampString);
	}
	
	
	/**
	 * java.sql.Timestamp转字符串
	 * @param time
	 * @param pattern
	 * @return
	 */
	public static String getSqlTimestampString(java.sql.Timestamp time,String pattern) {
		return new SimpleDateFormat(pattern).format(time);
	}
	
	
	/**
	 * localDateTime转字符串
	 * @param localDateTime
	 * @param patternOption
	 * @return
	 */
	public static String getLocalDateTimeString(LocalDateTime localDateTime,int patternOption) {
		DateTimeFormatter fmt;
		if(patternOption==12) {
			fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss:SSS a");	//十二小时制		
		}else if(patternOption==24) {
			fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss:SSS");	//二十四小时制	
		}else {
			return "格式不正确";
		}
		
        return localDateTime.format(fmt);      
	}
	
	
	/**
	 * localDate转字符串
	 * @param localDate
	 * @param pattern
	 * @return
	 */
	public static String getLocalDateString(LocalDate localDate,String pattern) {
	    DateTimeFormatter fmt = DateTimeFormatter.ofPattern(pattern);	    
		return localDate.format(fmt);  
	}
	
	
	/**
	 * java.sql.Date转字符串
	 * @param sqlDate
	 * @return
	 */
	public static String getSqlDateString(java.sql.Date sqlDate) {
		return sqlDate.toString();
	}	
	
	
	/**
	 * java.util.Date转字符串
	 * @param utilDate
	 * @param pattern
	 * @return
	 */
	public static String getUtilDateString(java.util.Date utilDate,String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(utilDate);
	}	
	
	
	
	
	
}
